//
//  LDHomeNextViewController.m
//  dmao-customer
//
//  Created by macbook on 2017/10/16.
//  Copyright © 2017年 秦慕乔. All rights reserved.
//

#import "LDHomeNextViewController.h"
#import "LineDownHomeTCell.h"
#import "LDStoreViewController.h"

@interface LDHomeNextViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIButton *SearchBtn;
@property (weak, nonatomic) IBOutlet UIButton *FirstBtn;
@property (weak, nonatomic) IBOutlet UIButton *SecondBtn;
@property (weak, nonatomic) IBOutlet UIButton *ThirdBtn;
@property (weak, nonatomic) IBOutlet UIView *LineView;
@property (strong, nonatomic) UITableView * tableView;
@property (strong, nonatomic) UIView *footerView;
@property (strong, nonatomic) NSMutableArray *ListArr;
@end

@implementation LDHomeNextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.titleLabel.text = self.titleStr;
    _ListArr = [[NSMutableArray alloc] init];
    [_FirstBtn setTitle:self.titleStr forState:UIControlStateNormal];
    
    _SearchBtn.layer.borderColor = [[UIColor colorWithHex:0xf2f2f2]CGColor];
    _SearchBtn.layer.borderWidth = 1.0f;
    [_SearchBtn.layer setCornerRadius:3.0];
    _SearchBtn.layer.masksToBounds = YES;
    _FirstBtn.enabled = NO;
    _ThirdBtn.enabled = NO;
    [self downloadData:@"1"];
    [self createFooterView];
    [self createTableView];
}

- (void)createFooterView
{
    _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 1)];
    _footerView.backgroundColor = RGB(232, 234, 234);
}

- (void)createTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 165, WIDTH, HEIGHT-165)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerNib:[UINib nibWithNibName:@"LineDownHomeTCell" bundle:nil] forCellReuseIdentifier:@"LineDownHomeTCell"];
    [_tableView setTableFooterView:_footerView];
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
}

- (void)downloadData:(NSString *)order
{
    [self.view loadingOnAnyView];
    if (!self.classId) {
        [self.view removeAnyView];
        return;
    }
    NSDictionary *dic = @{@"class_id":self.classId,@"jwd":[Utils getJWD],@"order":order};
    [THWManager PostHttp:API(LDTopNext) Parameters:dic Success:^(id responseObject) {
        [self.view removeAnyView];
        _ListArr = [[[Utils getData:responseObject] objectForKey:@"list"] objectForKey:@"Rows"];
        if (_ListArr.count == 0) {
            [self.view Message:@"暂无数据"];
        }
        [_tableView reloadData];
        
    } andFalse:^(NSError *error) {
        [self.view removeAnyView];
    }];
}

#pragma mark - UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _ListArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LineDownHomeTCell*cell = [tableView dequeueReusableCellWithIdentifier:@"LineDownHomeTCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[LineDownHomeTCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"LineDownHomeTCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell cellConfigureModel:_ListArr[indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 111;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LDStoreViewController *store = [[LDStoreViewController alloc] init];
    store.lineId = [_ListArr[indexPath.row] objectForKey:@"lineId"];
    [self.navigationController pushViewController:store animated:YES];
}

- (IBAction)SearchClick:(UIButton *)sender {
}
- (IBAction)SecondBtnClick:(UIButton *)sender {
    //初始化提示框；
    /**
     preferredStyle参数：
     UIAlertControllerStyleActionSheet,
     UIAlertControllerStyleAlert
     
     *  如果要实现ActionSheet的效果，这里的preferredStyle应该设置为UIAlertControllerStyleActionSheet，而不是UIAlertControllerStyleAlert；
     */
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"排序" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    /**
     *  style参数：
     UIAlertActionStyleDefault,
     UIAlertActionStyleCancel,
     UIAlertActionStyleDestructive（默认按钮文本是红色的）
     *
     */
    //分别按顺序放入每个按钮；
    [alert addAction:[UIAlertAction actionWithTitle:@"距离" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //点击按钮的响应事件；
        [_SecondBtn setTitle:@"距离" forState:UIControlStateNormal];
        [self downloadData:@"1"];
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"让利比" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //点击按钮的响应事件；
        [_SecondBtn setTitle:@"让利比" forState:UIControlStateNormal];
        [self downloadData:@"2"];
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        //点击按钮的响应事件；
    }]];
    
    //弹出提示框；
    [self presentViewController:alert animated:true completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
